<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="退款方式" prop="refundWay">
        <el-select
          v-model="queryParams.refundWay"
          placeholder="请选择退款方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_refund_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="refundList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_oder_type"
            :value="scope.row.orderType"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_order_status"
            :value="scope.row.auditStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="退款方式" align="center" prop="refundWay">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_refund_type"
            :value="scope.row.refundWay"
          />
        </template>
      </el-table-column>

      <el-table-column label="退款金额" align="center" prop="refundAmount" >
        <template slot-scope="scope">
            {{
              (scope.row.refundAmount / 100).toLocaleString(undefined, {
                minimumFractionDigits: 0,
                maximumFractionDigits: 2,
              })
            }}
          </template>
      </el-table-column>
      <!-- 反馈内容列，设置最多显示4行 -->
      <!-- <el-table-column label="退款原因" align="center" prop="content">
        <template slot-scope="scope">
          <div class="content-cell" :title="scope.row.content">
            {{ scope.row.content }}
          </div>
        </template>
      </el-table-column> -->

      <el-table-column label="申请时间" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:refund:edit']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="订单号" disabled />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <!-- v-model="form.refundAmount" -->
          <el-input
         
            placeholder="请输入退款金额"
            :value="formattedRefundAmount"
            disabled
          />
        </el-form-item>
        <el-form-item label="申请时间" prop="refundTime">
          <el-date-picker
            clearable
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退款申请时间"
            disabled
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失败原因" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
            disabled
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm(AuditStatus.SUCCESS)"
          :disabled="form.auditStatus != AuditStatus.PENDING"
          >审核成功</el-button
        >
        <el-button
          type="primary"
          @click="submitForm(AuditStatus.FAILED)"
          :disabled="form.auditStatus != AuditStatus.PENDING"
          >审核失败</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRefund,
  getRefund,
  delRefund,
  addRefund,
  updateRefund,
} from "@/api/order/refund";

export default {
  name: "Refund",
  dicts: ["sys_oder_type", "sys_refund_type", "sys_order_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款申请表格数据
      refundList: [],
      AuditStatus: {
        PENDING: 0,
        SUCCESS: 1,
        FAILED: 2,
      },

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        orderNo: null,
        orderType: null,
        auditStatus: null,
        refundWay: null,
        refundAmount: null,
        refundTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" },
        ],
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" },
        ],
        refundWay: [
          { required: true, message: "退款方式不能为空", trigger: "blur" },
        ],
        refundAmount: [
          { required: true, message: "退款金额不能为空", trigger: "blur" },
        ],
        refundTime: [
          { required: true, message: "退款时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    // 计算属性，返回格式化后的退款金额
    formattedRefundAmount() {
      return (this.form.refundAmount / 100).toLocaleString(undefined, {
        minimumFractionDigits: 0,
        maximumFractionDigits: 2,
      });
    },
  },
  methods: {
    /** 查询退款申请列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then((response) => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNo: null,
        orderType: null,
        auditStatus: null,
        refundWay: null,
        auditorName: null,
        refundAmount: null,
        content: null,
        refundTime: null,
        deleted: null,
        id: null,
        auditorId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退款申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row;
      this.open = true;
      this.title = "修改退款申请";
    },
    /** 提交按钮 */
    submitForm(auditStatus) {

      this.form.auditStatus = auditStatus;
      this.form.refundWay=1
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRefund(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除退款申请编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRefund(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/refund/export",
        {
          ...this.queryParams,
        },
        `refund_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
